import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import {TagDto} from './dto/tag.dto'
import { Tag} from './entities/tag.entity'
@Injectable()
export class TagService {
  constructor(
    @InjectModel('Tags') private readonly tagModel:Model<Tag>
) {}
  async create(tagDto: TagDto) {
    return await this.tagModel.insertMany(tagDto);
  }
 
  async findAll(userId: string):Promise<any> {
    return await this.tagModel.find({userId:userId});
  }

  async findOne(name: string, id: string):Promise<TagDto[]> {
    if(id)return await this.tagModel.find({_id:id});
    if(name)return await this.tagModel.find({name:name});
  }

  async update(id: string, tagDto: TagDto) {
    return await this.tagModel.updateOne({_id:id}, tagDto);
  }

  async remove(id: string):Promise<any> {
    return await this.tagModel.deleteOne({_id:id});
  }

  async list(page: number, pageSize: number, userId: string):Promise<any> {
    const skip = (page - 1) * pageSize;
    const data = await this.tagModel.find({userId:userId}).sort({ time: -1 })
    const list = await this.tagModel.find({userId:userId}).sort({ time: -1 }).skip(skip).limit(pageSize)
    return {total: data.length, list: list}
  }
}
